//
//  BYMASConstraintMaker.h
//  BYMasonry
//
//  Created by Jonas Budelmann on 20/07/13.
//  Copyright (c) 2013 cloudling. All rights reserved.
//

#import "BYMASConstraint.h"
#import "BYMASUtilities.h"

typedef NS_OPTIONS(NSInteger, MASAttribute) {
    BYMASAttributeLeft = 1 << NSLayoutAttributeLeft,
    BYMASAttributeRight = 1 << NSLayoutAttributeRight,
    BYMASAttributeTop = 1 << NSLayoutAttributeTop,
    BYMASAttributeBottom = 1 << NSLayoutAttributeBottom,
    BYMASAttributeLeading = 1 << NSLayoutAttributeLeading,
    BYMASAttributeTrailing = 1 << NSLayoutAttributeTrailing,
    BYMASAttributeWidth = 1 << NSLayoutAttributeWidth,
    BYMASAttributeHeight = 1 << NSLayoutAttributeHeight,
    BYMASAttributeCenterX = 1 << NSLayoutAttributeCenterX,
    BYMASAttributeCenterY = 1 << NSLayoutAttributeCenterY,
    BYMASAttributeBaseline = 1 << NSLayoutAttributeBaseline,

    BYMASAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline,
    BYMASAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline,
    
#if TARGET_OS_IPHONE || TARGET_OS_TV
    
    BYMASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin,
    BYMASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin,
    BYMASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin,
    BYMASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin,
    BYMASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin,
    BYMASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin,
    BYMASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins,
    BYMASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins,

#endif
    
};

/**
 *  Provides factory methods for creating BYMASConstraints.
 *  Constraints are collected until they are ready to be installed
 *
 */
@interface BYMASConstraintMaker : NSObject

/**
 *	The following properties return a new BYMASViewConstraint
 *  with the first item set to the makers associated view and the appropriate BYMASViewAttribute
 */
@property (nonatomic, strong, readonly) BYMASConstraint *left;
@property (nonatomic, strong, readonly) BYMASConstraint *top;
@property (nonatomic, strong, readonly) BYMASConstraint *right;
@property (nonatomic, strong, readonly) BYMASConstraint *bottom;
@property (nonatomic, strong, readonly) BYMASConstraint *leading;
@property (nonatomic, strong, readonly) BYMASConstraint *trailing;
@property (nonatomic, strong, readonly) BYMASConstraint *width;
@property (nonatomic, strong, readonly) BYMASConstraint *height;
@property (nonatomic, strong, readonly) BYMASConstraint *centerX;
@property (nonatomic, strong, readonly) BYMASConstraint *centerY;
@property (nonatomic, strong, readonly) BYMASConstraint *baseline;

@property (nonatomic, strong, readonly) BYMASConstraint *firstBaseline;
@property (nonatomic, strong, readonly) BYMASConstraint *lastBaseline;

#if TARGET_OS_IPHONE || TARGET_OS_TV

@property (nonatomic, strong, readonly) BYMASConstraint *leftMargin;
@property (nonatomic, strong, readonly) BYMASConstraint *rightMargin;
@property (nonatomic, strong, readonly) BYMASConstraint *topMargin;
@property (nonatomic, strong, readonly) BYMASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) BYMASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) BYMASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) BYMASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) BYMASConstraint *centerYWithinMargins;

#endif

/**
 *  Returns a block which creates a new BYMASCompositeConstraint with the first item set
 *  to the makers associated view and children corresponding to the set bits in the
 *  MASAttribute parameter. Combine multiple attributes via binary-or.
 */
@property (nonatomic, strong, readonly) BYMASConstraint *(^attributes)(MASAttribute attrs);

/**
 *	Creates a BYMASCompositeConstraint with type BYMASCompositeConstraintTypeEdges
 *  which generates the appropriate BYMASViewConstraint children (top, left, bottom, right)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) BYMASConstraint *edges;

/**
 *	Creates a BYMASCompositeConstraint with type BYMASCompositeConstraintTypeSize
 *  which generates the appropriate BYMASViewConstraint children (width, height)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) BYMASConstraint *size;

/**
 *	Creates a BYMASCompositeConstraint with type BYMASCompositeConstraintTypeCenter
 *  which generates the appropriate BYMASViewConstraint children (centerX, centerY)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) BYMASConstraint *center;

/**
 *  Whether or not to check for an existing constraint instead of adding constraint
 */
@property (nonatomic, assign) BOOL updateExisting;

/**
 *  Whether or not to remove existing constraints prior to installing
 */
@property (nonatomic, assign) BOOL removeExisting;

/**
 *	initialises the maker with a default view
 *
 *	@param	view	any BYMASConstraint are created with this view as the first item
 *
 *	@return	a new BYMASConstraintMaker
 */
- (id)initWithView:(MAS_VIEW *)view;

/**
 *	Calls install method on any BYMASConstraints which have been created by this maker
 *
 *	@return	an array of all the installed BYMASConstraints
 */
- (NSArray *)install;

- (BYMASConstraint * (^)(dispatch_block_t))group;

@end
